package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.entity.Article;
import com.itany.entity.ArticleType;
import com.itany.service.ArticleTypeService;
import com.itany.utils.SFTPUtils;
import com.itany.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:yaozhiyuan0117@163.com
 * Time:2019/12/20 15:13
 * Describe:
 * version:1.0
 */

@RestController
@RequestMapping("/articletypes")
public class ArticleTypeController {

    @Autowired
    private ArticleTypeService articleTypeService;

    @RequestMapping("/findByPage")
    public Map findByPage(@RequestParam(defaultValue = "1") Integer page, Integer rows, HttpServletRequest request){

        PageInfo<ArticleType> info = articleTypeService.findByPage(page, rows);

        Map map = new HashMap();
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    @RequestMapping("/addArticleType")
    public ResponseResult addType(@RequestParam("name_add") String name, @RequestParam("imgurl_add") MultipartFile file)  {
        ArticleType type = new ArticleType();

        // 1.上传图片到SFTP服务器
        String fileName = file.getOriginalFilename();
        fileName=System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
        System.out.println("-------------");
        System.out.println("fileName "+ fileName);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String  uploadDir ="yzy/" +dateFormat.format(new Date())+"/";
        try {
            SFTPUtils.sftpPut("192.168.4.8","tyh","123",22,uploadDir,file.getInputStream(),fileName);
        } catch (Exception e) {
            // e.printStackTrace();
            System.out.println(e.getMessage());
            System.out.println("访问远程文件服务器出错！");
        }

        // 2.获取上传路径
        String imgurl = uploadDir + fileName;
        type.setName(name);
        type.setImgurl(imgurl);
        // 文件上传，添加url
        articleTypeService.addType(type);
        return ResponseResult.success();
    }

    @RequestMapping("/findAll")
    public List<ArticleType> findAll(){
        List<ArticleType> types = articleTypeService.findAll();
        ArticleType type = new ArticleType();
        // type.setId(0);
        // type.setName("全部");
        // types.add(type);
        return types;
    }


    @RequestMapping("/findById")
    public ResponseResult findById(Integer id){
        ArticleType type = articleTypeService.findById(id);
        return ResponseResult.success(type);
    }

    @RequestMapping("/modifyArticleType")
    public ResponseResult modifyArticleType(@RequestParam("id_modify") Integer id, @RequestParam("name_modify") String name, @RequestParam("imgurl_modify") MultipartFile file)  {
        ArticleType type = new ArticleType();
        System.out.println("--------------");
        System.out.println(file);
        //即使前台没有传递图片，对应的file也有内容，因此需要用file.isEmpty进行判断。
        if (! file.isEmpty()) {
            // 1.上传图片到SFTP服务器
            String fileName = file.getOriginalFilename();
            fileName=System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
            System.out.println("-------------");
            System.out.println("fileName "+ fileName);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            String  uploadDir ="yzy/" +dateFormat.format(new Date())+"/";
            try {
                SFTPUtils.sftpPut("192.168.4.8","tyh","123",22,uploadDir,file.getInputStream(),fileName);
            } catch (Exception e) {
                // e.printStackTrace();
                System.out.println(e.getMessage());
                System.out.println("访问远程文件服务器出错！");
            }

            // 2.获取上传路径
            String imgurl = uploadDir + fileName;
            type.setImgurl(imgurl);
        }


        type.setId(id);
        type.setName(name);

        // 文件上传，添加url
        articleTypeService.modifyArticleType(type);
        return ResponseResult.success();
    }

}
